/*
 * Author: Alvin
 * Modified By: Alvin
 * Created Date: 2024-05-13 16:08:43
 * Last Modified: 2024-05-13 18:29:56
 * Description:
 */
import 'package:yt_devops/low_code/model/form_config_model.dart';
import 'package:yt_devops/low_code/model/services_config_model.dart';
import 'package:yt_devops/low_code/widgets/singleForm/models/low_code_single_form_example.dart';

class LowCodeSingleFormAnalyzer {
  String title = "单表单示例-参照新建站点页";
  bool canSubmit = false;
  List initDataList = [];
  ServicesConfigModel initDataConfig;
  ServicesConfigModel updateDataConfig;
  ServicesConfigModel commitDataConfig;
  FormConfigModel formConfig;

  LowCodeSingleFormAnalyzer({
    required this.title,
    required this.canSubmit,
    required this.initDataList,
    required this.initDataConfig,
    required this.updateDataConfig,
    required this.commitDataConfig,
    required this.formConfig,
  });

  // 本地example示例
  factory LowCodeSingleFormAnalyzer.analyzeLocalModel(LowCodeSingleFormExample example) {
    return LowCodeSingleFormAnalyzer(
      title: example.title,
      canSubmit: example.canSubmit,
      initDataList: example.initDataList,
      initDataConfig: ServicesConfigModel.fromJson(Map.from(example.servicesConfig['initDataService'])),
      updateDataConfig: ServicesConfigModel.fromJson(Map.from(example.servicesConfig['updateDataService'])),
      commitDataConfig: ServicesConfigModel.fromJson(Map.from(example.servicesConfig['saveDataService'])),
      formConfig: FormConfigModel.fromJsonList(example.formConfig),
    );
  }

  // web example示例
  factory LowCodeSingleFormAnalyzer.analyzeWebExample(Map<String, dynamic> json) {
    return LowCodeSingleFormAnalyzer(
      title: json['componentList'][0]['chartConfig']['title'],
      canSubmit: true,
      initDataList: [],
      initDataConfig: ServicesConfigModel.fromJson(
        {
          "url": json['componentList'][0]['request']['requestUrl'],
          "method": json['componentList'][0]['request']['requestHttpType'],
          "params": json['componentList'][0]['request']['requestParams']['Params'],
        },
      ),
      updateDataConfig: ServicesConfigModel.fromJson(
        {
          "url": json['componentList'][0]['request']['requestUrl'],
          "method": json['componentList'][0]['request']['requestHttpType'],
          "params": json['componentList'][0]['request']['requestParams']['Params'],
        },
      ),
      commitDataConfig: ServicesConfigModel.fromJson(
        {
          "url": json['componentList'][0]['request']['requestUrl'],
          "method": json['componentList'][0]['request']['requestHttpType'],
          "params": json['componentList'][0]['request']['requestParams']['Params'],
        },
      ),
      formConfig: FormConfigModel.fromWebJsonList(List.from(json['componentList'][0]['option']['dataset']['source'])),
    );
  }
}
